package org.theSeed.fileContract.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import org.theSeed.base.utils.SeedPathUtil;
import org.theSeed.base.utils.SeedStringUtil;

import java.net.URLEncoder;
import java.util.Arrays;

/**
 * 文件配置工具
 */
public class SeedFileConfigUtil {
    /**
     * 创建web路径资源id
     * @param relativePath
     * @return
     */
    public static String createWebPathResourceId(String relativePath,String extName) {
        String fileName = ObjectUtil.isNotEmpty(extName)?IdUtil.getSnowflake().nextIdStr() + "." + extName :IdUtil.getSnowflake().nextIdStr();
        if (ObjectUtil.isNotEmpty(relativePath))
            return SeedStringUtil.concatStr(Arrays.asList(relativePath, fileName),"/");
        else
            return fileName;
    }

    /**
     * 创建web参数的资源id
     * @param relativePath
     * @return
     */
    public static String createWebParamResourceId(String relativePath,String extName) {
        String fileName = ObjectUtil.isNotEmpty(extName)?IdUtil.getSnowflake().nextIdStr() + "." + extName :IdUtil.getSnowflake().nextIdStr();
        if (ObjectUtil.isNotEmpty(relativePath))
            return URLEncoder.encode(SeedPathUtil.concatPath(Arrays.asList(relativePath, fileName)));
        else
            return fileName;
    }

    /**
     * 创建唯一标识
     * @param contractType
     * @param contractTypeInstance
     * @param resourceId
     * @return
     */
    public static String createChipKey(String contractType, String contractTypeInstance, String resourceId) {
        return contractType + contractTypeInstance + ":" + resourceId;
    }
}
